package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 保存
     *
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        //调用service保存
        setmealService.saveWithDish(setmealDto);
        return R.success("保存套餐成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        //1. 分类的分页查询
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage, lqw);

        //2. 创建Page<SetmealDto>分页对象
        Page<SetmealDto> dtoPage = new Page<>();
        // 复制分页数据
        BeanUtils.copyProperties(setmealPage, dtoPage, "records");

        //3. 处理分页集合数据
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> dtoList = new ArrayList<>();
        // 遍历
        records.forEach(setmeal -> {
            //创建SetmealDto对象
            SetmealDto dto = new SetmealDto();
            // 复制setmeal属性 --> dto中
            BeanUtils.copyProperties(setmeal, dto);
            // 查询分类信息
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                dto.setCategoryName(category.getName());
            }
            dtoList.add(dto);
        });

        //4. 将处理后集合, 添加到分页对象中
        dtoPage.setRecords(dtoList);
        return R.success(dtoPage);
    }

    /*
        allEntries = true: 清空"setmealCache"下的所有缓存
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.removeByDish(ids);

        return R.success("删除分类成功");
    }

    /**
     * 查询套餐集合
     *  Redis 持久化方案:  RDB AOF
     *        持久化: 将内存数据写出到磁盘 --> IO流 -->  对象从内存写出到磁盘, 对象必须实现序列化接口!!!
     *
     *
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId")
    @GetMapping("list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        //1. 条件构建器
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);

        //2. 查询
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
